package com.siyoumi.app.modules.app_book.service.handle_order;

import com.siyoumi.app.entity.BookOrder;
import com.siyoumi.app.entity.BookStore;
import com.siyoumi.app.entity.SysSale;
import com.siyoumi.app.modules.app_book.service.BookOrderHandleAbs;
import com.siyoumi.app.modules.app_book.service.SvcBookStore;
import com.siyoumi.app.modules.user.service.SvcSysSale;
import com.siyoumi.component.LogPipeLine;
import com.siyoumi.component.XApp;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;

import java.math.BigDecimal;

/**
 * 分帐，平台分账，主播分帐
 */
public class BookOrderHandle90SharePrice
        extends BookOrderHandleAbs {
    static public BookOrderHandle90SharePrice of() {
        return new BookOrderHandle90SharePrice();
    }

    @Override
    public XReturn orderBeforeCheck() {

        return EnumSys.OK.getR();
    }

    @Override
    public String getTitle() {
        return "平台分销员分帐";
    }

    @Override
    public XReturn handle(BookOrder entityOrder) {
        LogPipeLine log = getLog();

        if (entityOrder.getBorder_edit() == 0) { //修改订单不更新分销员
            BookStore entityStore = SvcBookStore.getApp().getEntity(entityOrder.getBorder_store_id());
            //赋值分成比率
            {
                //平台
                entityOrder.setBorder_share_app_rate(entityStore.getBstore_app_rate());
                //分销员
                {
                    entityOrder.setBorder_share_sales_rate(BigDecimal.ZERO);
                    SysSale entitySale;
                    if (XStr.hasAnyText(getVo().getSale_uid())) { //指定分销员
                        entitySale = SvcSysSale.getBean().getEntitySaleByUid(getVo().getSale_uid());
                    } else { //用户绑定分销员
                        entitySale = SvcSysSale.getBean().getEntitySale(entityOrder.getBorder_uid());
                    }
                    if (entitySale != null) {
                        if (entitySale.getSale_state() == 1) { //有效分销员才能分佣金
                            entityOrder.setBorder_share_sales_uid(entitySale.getSale_uid());
                            entityOrder.setBorder_share_sales_level(entitySale.getSale_level());
                            entityOrder.setBorder_share_sales_name(entitySale.getSale_name());

                            entityOrder.setBorder_share_sales_rate(SvcBookStore.getSalesRate(entityStore, entitySale.getSale_level()));
                        } else {
                            log.setLogMsg(entitySale.getSale_uid() + "：无效分销员");
                        }
                    }

                }
            }
        }

        //计算分成金额
        matchSharePrice(entityOrder);
        //检查积分折扣
        if (entityOrder.getBorder_price_down_fun().compareTo(entityOrder.getBorder_share_app_price()) > 0) {
            XValidator.err(20061, "积分使用金额异常");
        }

        return EnumSys.OK.getR();
    }

    /**
     * 根据分成比率，计算分成金额
     *
     * @param entityOrder
     */
    static public void matchSharePrice(BookOrder entityOrder) {
        LogPipeLine log = LogPipeLine.ofSys();

        BigDecimal priceShareFinal = entityOrder.getBorder_price_share_final();
        entityOrder.setBorder_share_app_price(BigDecimal.ZERO);
        entityOrder.setBorder_share_sales_price(BigDecimal.ZERO);
        entityOrder.setBorder_share_store_price(BigDecimal.ZERO);
        if (priceShareFinal.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        log.setLogMsg("分账总金额：" + priceShareFinal);
        //平台
        if (entityOrder.getBorder_share_app_rate().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal appPrice = XApp.toDecimalPay(entityOrder.getBorder_price_share_final().multiply(XApp.decimal100(entityOrder.getBorder_share_app_rate())));
            appPrice = appPrice.subtract(entityOrder.getBorder_price_down_fun()); //扣减积分抵扣
            if (appPrice.compareTo(BigDecimal.ZERO) <= 0) { //少于0，就不分帐
                appPrice = BigDecimal.ZERO;
            }
            entityOrder.setBorder_share_app_price(appPrice);
            log.setLogMsg("积分抵扣：" + entityOrder.getBorder_price_down_fun());
            log.setLogMsg("平台分账：" + appPrice);
            priceShareFinal = priceShareFinal.subtract(appPrice);
        }

        //分销员
        if (entityOrder.getBorder_share_sales_rate().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal salesPrice = XApp.toDecimalPay(entityOrder.getBorder_price_share_final().multiply(XApp.decimal100(entityOrder.getBorder_share_sales_rate())));
            log.setLogMsg("分销员分账：" + salesPrice);
            entityOrder.setBorder_share_sales_price(salesPrice);
            priceShareFinal = priceShareFinal.subtract(salesPrice);
        }
        log.setLogMsg("商家分账：" + priceShareFinal);
        entityOrder.setBorder_share_store_price(priceShareFinal);
    }
}
